package com.xinchuang.exception;

import com.xinchuang.common.Result;
import com.xinchuang.common.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * TODO
 *
 * @author zkwczx
 * @version 1.0
 * @date 2024/4/21 15:42
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /********************************
     *  @function  : 自定义从捕捉
     *  @parameter : [e:CustomException | 自定义异常]
     *  @date      : 2023/12/5 11:47
     ********************************/
    @ExceptionHandler(value = CustomException.class)
    public Result customExceptionHandler(HttpServletRequest request, CustomException e) {
        log.error("业务异常，url：{}, 异常内容：{}" ,request.getRequestURI(), e);
        return ResultUtil.error(e.getCode() , e.getMessage());
    }

    /********************************
     *  @function  : 空指针异常捕捉
     *  @parameter : [e：Exception | 异常]
     *  @date      : 2023/12/5 11:47
     ********************************/
    @ExceptionHandler(value = Exception.class)
    public Result exceptionHandler(HttpServletRequest request, Exception e) {
        log.error("服务器内部异常异常，url：{}, 异常内容：{}" ,request.getRequestURI(), e);
        return ResultUtil.error(500 , "系统异常，请稍等重试");
    }

}
